﻿<table class="unit-table shopping-cart-item">
    <thead>
        <tr>
            <th>@T(Res.In("Common").Of("Vocab").Name("SKU"))</th>
            <th>@T(Res.Name("Quantity"))</th>
            @if(!Model.IsOverview)
            {
                <th>@T(Res.Name("Price"))</th>
            }
            <th>@T(Res.Name("Total"))</th>
            @if(!Model.IsOverview)
            {
                <th></th>
            }
        </tr>
    </thead>
    <tbody>
    @foreach(var i in Model.Items)
    {
        <tr class="unit shopping-cart-item">
            <td>
                <ul class="bar inlinebar">
                    <li><a class="confirm" href="@Url.Action(Loc.Act("Remove").Set(new
                    {
                        cartItemID = JDM.Int32(i.EntityID),
                        quantity = JDM.Decimal(i.Quantity)
                    }).Localized())">X</a></li>
                </ul>
                <a href="@Url.Action(Loc.Act("Show").Ctrl("Item").Area("Store").Set(new
                {
                    id = JDM.Int32(i.SalesItem.EntityID)
                }).Localized())" title="@JDM.String(i.SalesItem.SKU)">@JDM.String(i.SalesItem.SKU)</a>
                @if(!Model.IsOverview)
                {
                    var options = i.Options as IEnumerable<dynamic>;
                    if(options.Any())
                    {
                        <table class="key-value">
                        @foreach(var g in options.GroupBy(x => JDM.String(x.CodeName)))
                        {
                            var t_first = (g.First().Translations as IEnumerable<dynamic>)
                                .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                                    JDM.Int32(Svc.Localization.Language.EntityID));
                            <tr>
                                <td>
                                    <label>@(t_first != null ? JDM.String(t_first.Name) : g.Key)</label>
                                </td>
                                <td>
                                    <ul class="bar">
                                    @{
                                        var n = 0;
                                        foreach(var o in g)
                                        {
                                            var t_opt = (o.Translations as IEnumerable<dynamic>)
                                                .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                                                    JDM.Int32(Svc.Localization.Language.EntityID));
                                            <li>
                                                @T(Res.Name("Option"), 
                                                    t_opt != null
                                                        ? JDM.String(t_opt.Value) 
                                                        : String.Empty, 
                                                    Svc.Price((decimal)JDM.Decimal(o.Price)))
                                            </li>
                                            if(n < g.Count() - 1)
                                            {
                                                <li class="separator">,</li>
                                            }
                                            n++;
                                        }
                                    }
                                    </ul>
                                </td>
                            </tr>
                        }
                        </table>
                    }
                }
            </td>
            <td>@Svc.Numeric((decimal)JDM.Decimal(i.Quantity))</td>
            @if(!Model.IsOverview)
            {
                <td>@Svc.Price((decimal)JDM.Decimal(i.Price))</td>
            }
            <td>@Svc.Price((decimal)JDM.Decimal(i.Total))</td>
        </tr>
    }
    </tbody>
    <tfoot>
        <tr>
            @if(!Model.IsOverview)
            {
                <td></td>
            }
            <td colspan="3">
                @Svc.Price((Model.Items as IEnumerable<dynamic>).Sum(x => (decimal)JDM.Decimal(x.Total)))
            </td>
        </tr>
    </tfoot>
</table>